<?php
/**
 * @title 257. 二叉树的所有路径 - 递归
 * @author start2004
 */

// ini_set("memory_limit", "128M");

class Solution {

    /**
     * @param TreeNode $root
     * @return String[]
     */
    function binaryTreePaths($root) {
        if($root === null){
            return [];
        } else {}

        /**
         * @since 2020-09-04 路径数组
         */
        $result = self::find($root, []);

        /**
         * @since 2020-09-04 转成箭头
         */
        foreach ($result as $key=>$list){
            $result[$key] = implode("->", $list);
        }

        /**
         * @return
         */
        return $result;
    }

    /**
     * @title 递归每条路径
     * @author start2004
     * @since 2020-09-04 Created
     *
     * @param $root
     * @param array $list
     * @return array
     */
    public function find($root, $list = []){
        /**
         * @since 2020-09-04 结果集
         */
        $result = [];

        /**
         * @since 2020-09-04 增加根节点值
         */
        $list[] = $root->val;

        /**
         * @since 2020-09-04 叶子节点
         */
        if($root->left === null AND $root->right === null){
            $result[] = $list;
        } else {
            /**
             * @since 2020-09-04 左节点递归
             */
            $left = [];
            if($root->left !== null){
                $left = self::find($root->left, $list);
            } else {}

            /**
             * @since 2020-09-04 右节点递归
             */
            $right = [];
            if($root->right !== null) {
                $right = self::find($root->right, $list);
            } else {}

            /**
             * @since 2020-09-04 合并
             */
            $result = array_merge($left, $right);
        }

        /**
         * @return
         */
        return $result;
    }
}



/**
 * @url http://127.0.0.1/leetcode/202009/2020.09.04.php
 */
$datas = [
    [1,2,3,null,5],
    [1,2,3,4,5],
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    var_dump($data);

    $obj = new Solution();
    $result = $obj->binaryTreePaths($xhprof->tree($data));
    var_dump($result);
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $result = $obj->$data[0][$i](...$data[1][$i]);
//
//         echo $data[0][$i] ."(\"". $data[1][$i][0] ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
